package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);

    //入口
    public void start() {
        //死循环
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //键盘录入
            //switch 选择
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudents();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入有误重新输入");
            }
        }
    }

    //新增
    public void addStudent() {

        //录入信息
        System.out.println("学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {
                System.out.println("存在重录");
            } else {
                break;
            }
        }
        System.out.println("姓名");
        String name = sc.next();
        System.out.println("年龄");
        int age = sc.nextInt();
        System.out.println("生日");
        String birthday = sc.next();
        //封装对象
        Student student = new Student(sid, name, age, birthday);
        //调用service
        studentService.addStudent(student);
        System.out.println("新增成功");


    }

    public void findAllStudents() {
        ArrayList<Student> list = studentService.findAllStudents();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student != null) {
                System.out.println(student.getSid() + "\t" +
                        student.getName() + "\t" + student.getAge() + "岁\t" + student.getBirthday());
            }
        }
    }

    public void deleteStudent() {
        //录学号
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //判断是否存在在  存在 调用service 删
            boolean exists = studentService.isExists(sid);
            if (exists) {
                break;
            } else {   //不存在重录
                System.out.println("不存在,重录");
            }
        }

        studentService.deleteStudent(sid);
        System.out.println("删除成功");
    }

    public void updateStudent() {
        //录学号
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //判断是否存在在  存在 调用service 删
            boolean exists = studentService.isExists(sid);
            if (exists) {
                break;
            } else {   //不存在重录
                System.out.println("不存在,重录");
            }
        }
        System.out.println("姓名");
        String name = sc.next();
        System.out.println("年龄");
        int age = sc.nextInt();
        System.out.println("生日");
        String birthday = sc.next();
        //封装对象
        Student student = new Student(sid, name, age, birthday);
        studentService.updateStudent(sid, student);
        System.out.println("修改成功");
    }
}
